/**
 * 
 */
package com.example.radioclub.music.adapter;

import java.util.ArrayList;

import com.example.radioclub.R;
import com.example.radioclub.data.Constants;
import com.example.radioclub.types.Genre;
import com.example.radioclub.utilities.FunctionUtils;

import android.content.Context;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.TextureView;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseExpandableListAdapter;
import android.widget.TextView;

/**
 * This adapter maintain genre in list for searching feature.
 * 
 * @author wizard
 * 
 */
public class GenreAdapter extends BaseExpandableListAdapter {
	private final String TAG = Constants.APP_TAG + "Genre Adapter";
	private Context context;
	private ArrayList<Genre> genres;

	public GenreAdapter(Context context, ArrayList<Genre> array) {
		// TODO Auto-generated constructor stub
		this.context = context;
		this.genres = array;
	}

	public GenreAdapter(Context context, String genreListJson) {
		// TODO Auto-generated constructor stub
		this.context = context;
		this.genres = FunctionUtils.getGenreFromJSON(genreListJson);
	}

	@Override
	public Object getChild(int groupPosition, int childPosition) {
		// TODO Auto-generated method stub
		return genres.get(groupPosition).getGenres().get(childPosition);
	}

	@Override
	public long getChildId(int groupPosition, int childPosition) {
		// TODO Auto-generated method stub
		return Long.parseLong(this.genres.get(groupPosition).getGenres()
				.get(childPosition).getGenreId());
	}

	@Override
	public int getChildrenCount(int groupPosition) {
		// TODO Auto-generated method stub
		return this.genres.get(groupPosition).getGenres().size();
	}

	@Override
	public View getChildView(int groupPosition, int childPosition,
			boolean isLastChild, View convertView, ViewGroup parent) {
		// TODO Auto-generated method stub
		Genre genre = this.genres.get(groupPosition).getGenres()
				.get(childPosition);
		View view = LayoutInflater.from(this.context).inflate(
				R.layout.genre_layout, null, false);
		TextView genreName = (TextView) view.findViewById(R.id.genre_name);
		genreName.setText(genre.getGenreName());

		return view;
	}

	@Override
	public Object getGroup(int groupPosition) {
		// TODO Auto-generated method stub
		return this.genres.get(groupPosition);
	}

	@Override
	public int getGroupCount() {
		// TODO Auto-generated method stub
		return this.genres.size();
	}

	@Override
	public long getGroupId(int groupPosition) {
		// TODO Auto-generated method stub
		return Long.parseLong(this.genres.get(groupPosition).getGenreId());
	}

	@Override
	public View getGroupView(int groupPosition, boolean isExpanded,
			View convertView, ViewGroup parent) {
		// TODO Auto-generated method stub
		Genre genre = this.genres.get(groupPosition);
		View view = LayoutInflater.from(this.context).inflate(
				R.layout.genre_layout, null);
		TextView genreName = (TextView) view.findViewById(R.id.genre_name);
		genreName.setText(genre.getGenreName());

		return view;
	}

	@Override
	public boolean isChildSelectable(int groupPosition, int childPosition) {
		// TODO Auto-generated method stub
		return true;
	}

	@Override
	public boolean hasStableIds() {
		// TODO Auto-generated method stub
		return true;
	}

	@Override
	public boolean areAllItemsEnabled() {
		// TODO Auto-generated method stub
		return true;
	}

	@Override
	public void onGroupExpanded(int groupPosition) {
		// TODO Auto-generated method stub
		Log.i(TAG, this.genres.get(groupPosition).getGenres().size() + "");
	}

}
